package project.main;

import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Connection;
import project.dbaccesslayer.MySQLAccessLayer;
import project.dbaccesslayer.View_SO_Data_VW;

public class CreateSVMInputFile {

	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			System.out
					.println("Usage: CreateSVMInputFile <limit> <outputFIle>");
			System.out.println();
			System.out
					.println("       if <outputFIle> is - then output to STDOUT");
			return;
		}
		int limit = Integer.parseInt(args[0]);
		String outFile = args[1];

		try (Writer out = getOutput(outFile);
				Connection conn = MySQLAccessLayer.openConnectionToMySql()) {
			for (String line : View_SO_Data_VW.getData(conn, limit)) {
				out.write(line);
				out.write(System.lineSeparator());
			}
		}
	}

	private static Writer getOutput(String outFile) throws IOException {
		if ("-".equals(outFile)) {
			return new OutputStreamWriter(System.out);
		} else {
			return new FileWriter(outFile);
		}
	}
}
